﻿defClass(savefarmer.util, "Job", null,
function(Job, Job_prototype) {
    Job_prototype.init = function() {
        this.request = new savefarmer.util.Request();
        this.tools = new savefarmer.util.Tools();
    };

    //登录人人网
    Job_prototype.login = function(loginUserId, loginUserPwd) {
        var url = "http://www.renren.com/PLogin.do";
        var postData = "domain=renren.com&domain=renren.com&email=" + loginUserId + "&formName=&isplogin=truemethod=&origURL=&password=" + loginUserPwd + "&submit=登录";
        var loginResult = this.request.doPostRequest(url, postData, null);
        if (loginResult.indexOf("登录帐号或密码错误") > 0) {
            document.getElementById("login_msg").innerHTML = "&nbsp;";
            document.getElementById("login_error").innerHTML = "登录帐号或密码错误，请重新登录";
            return false;
        }
        return true;
    }

    //获得inuId
    Job_prototype.getInuId = function() {
        var url = "http://apps.renren.com/littlewar?origin=103";
        var littlewarURL = this.request.doGetRequest(url, null);
        littlewarURL = littlewarURL.substr(littlewarURL.indexOf('id="iframe_canvas" src="') + 24, littlewarURL.length);
        littlewarURL = littlewarURL.substr(0, littlewarURL.indexOf('"'));
        littlewarURL = littlewarURL.replace(/amp;/g, "");

        savefarmer.inuId = this.request.doGetRequest(littlewarURL, null);
        savefarmer.inuId = savefarmer.inuId.substr(savefarmer.inuId.indexOf('inuId=') + 6, savefarmer.inuId.length);
        savefarmer.inuId = savefarmer.inuId.substr(0, savefarmer.inuId.indexOf('&'));
    }

    //获得小小战争初始化信息
    Job_prototype.getInitInfo = function() {
        var url = "http://xnapi.lw.fminutes.com/api.php?inuId=" + savefarmer.inuId + "&method=Scene.init";
        var postData = 'data=null';
        var result = this.request.doPostRequest(url, postData, null);
        var initInfo = this.tools.getJSON(result);

        //取得我的头像
        url = "http://notify.renren.com/wpi/getonlinecount.do";
        postData = null;
        var result = this.request.doPostRequest(url, postData, null);
        var personInfo = this.tools.getJSON(result);

        //设置全局变量keyName
        savefarmer.keyName = initInfo.info.getKey.keyName;

        //设置全局变量requestSig
        savefarmer.requestSig = this.tools.getKey(initInfo.info.getKey.key, initInfo.info.getKey.keyName);

        //同步服务器时间
        savefarmer.difTime = initInfo.info.time - tools.getLocalTime() + Number(document.getElementById("delay_time").value);

        //添加用户ID
        initInfo.info.player_info["userId"] = personInfo.hostid;
        //添加用户姓名
        initInfo.info.player_info["userName"] = personInfo.hostname;
        //添加用户头像
        initInfo.info.player_info["headPic"] = personInfo.tinyurl;

        return initInfo;
    }

    //获得好友列表
    Job_prototype.getFriendData = function() {
        var url = "http://xnapi.lw.fminutes.com/api.php?inuId=" + savefarmer.inuId + "&method=Friend.run";
        var result = this.request.doPostRequest(url, null, null);
        var friends = this.tools.getJSON(result);
        var friendId = friends.info.gameData.data;
        var friendSlave = friends.info.gameData.slave;
        var friendMaster = friends.info.gameData.master;
        var friendInfo = friends.info.friend.friendInfo;
        var friendData = [];
        for (var key in friendId) {
            if (key != "1") {
                var friend = {};
                friend["uid"] = friendId[key].uid;
                friend["grade"] = friendId[key].grade;
                friend["flower"] = friendId[key].flower;
                //添加被我占领的好友标识
                for (var i = 0; i < friendSlave.length; i++) {
                    if (friendSlave[i] == friendId[key].uid) {
                        friend["slave"] = true;
                        break;
                    } else {
                        friend["slave"] = false;
                    }
                }
                //添加别人被占领的好友标识
                for (var i = 0; i < friendMaster.length; i++) {
                    if (friendMaster[i] == friendId[key].uid) {
                        friend["master"] = true;
                        break;
                    } else {
                        friend["master"] = false;
                    }
                }
                //添加头像及名称
                for (var i = 0; i < friendInfo.length; i++) {
                    if (friendInfo[i].uid == friendId[key].uid) {
                        friend["headPic"] = friendInfo[i].headPic;
                        friend["userName"] = friendInfo[i].userName;
                        break;
                    }
                }
				
				//如果好友被删除，那就不加入到好友列表
				if(!this.tools.isNotEmpty(friend["userName"])){
					continue;
				}
				
                friendData.push(friend);
            }
        }

        //按照级别降序
        friendData.sort(function(a, b) {
            return parseInt(a["grade"]) > parseInt(b["grade"]) ? -1 : 1
        });

        return friendData;
    };

    //获得好友详细信息
    Job_prototype.getFriendDetail = function(fid) {
        //累计api调用次数
        savefarmer.apiCount++;
        var url = "http://xnapi.lw.fminutes.com/api.php?inuId=" + savefarmer.inuId + "&method=Scene.run";
        var postData = 'data={"fid":"' + fid + '"}&keyName=' + savefarmer.keyName + '&requestSig=' + savefarmer.requestSig;
        var result = this.request.doPostRequest(url, postData, null);
        return this.tools.getJSON(result);
    }

    //收获士兵
    Job_prototype.gainRecruitment = function(id, fct, param) {
        //累计api调用次数
        savefarmer.apiCount++;
        var url = "http://xnapi.lw.fminutes.com/api.php?inuId=" + savefarmer.inuId + "&method=Produce.gainRecruitment";
        var postData = 'data={"id":' + id + '}&keyName=' + savefarmer.keyName + '&requestSig=' + savefarmer.requestSig;
        var result = this.request.doPostRequest(url, postData, fct, param);
        return this.tools.getJSON(result);
    }

    //训练士兵
    Job_prototype.recruiteSoldier = function(produceId, id, fct, param) {
        var soldierPlan;
        //设置自动练兵方案
        var plan = document.getElementsByName("radio_soldier");
        for (var i = 0; i < plan.length; i++) {
            if (plan[i].checked == true) {
                soldierPlan = plan[i].value;
            }
        }
        //累计api调用次数
        savefarmer.apiCount++;
        var url = "http://xnapi.lw.fminutes.com/api.php?inuId=" + savefarmer.inuId + "&method=Produce.recruiteSoldier";
        var postData = 'data={"produce_id":' + soldierPlan + ',"id":' + id + '}&keyName=' + savefarmer.keyName + '&requestSig=' + savefarmer.requestSig;
        var result = this.request.doPostRequest(url, postData, fct, param);
        return this.tools.getJSON(result);
    }

    //收获食物
    Job_prototype.gainFood = function(id, ids, fct, param) {
        //累计api调用次数
        savefarmer.apiCount++;
        var url = "http://xnapi.lw.fminutes.com/api.php?inuId=" + savefarmer.inuId + "&method=Produce.gainFood";
        var postData = 'data={"id":' + id + ',"ids":' + ids + '}&keyName=' + savefarmer.keyName + '&requestSig=' + savefarmer.requestSig;
        var result = this.request.doPostRequest(url, postData, fct, param);
        return this.tools.getJSON(result);
    }

    //拿取食物
    Job_prototype.stealingFood = function(factoryId, descId, ids, fct, param) {
        //累计api调用次数
        savefarmer.apiCount++;
        var url = "http://xnapi.lw.fminutes.com/api.php?inuId=" + savefarmer.inuId + "&method=Social.stealingFood";
        var postData = 'data={"desc_id":"' + descId + '","factory_id":' + factoryId + ',"ids":' + ids + '}&keyName=' + savefarmer.keyName + '&requestSig=' + savefarmer.requestSig;
        var result = this.request.doPostRequest(url, postData, fct, param);
        return this.tools.getJSON(result);
    }

    //生产食物
    Job_prototype.produceFood = function(produceId, id, fct, param) {
        var foodPlan;
        //设置自动播种方案
        var plan = document.getElementsByName("radio_food");
        for (var i = 0; i < plan.length; i++) {
            if (plan[i].checked == true) {
                foodPlan = plan[i].value;
            }
        }
        //累计api调用次数
        savefarmer.apiCount++;
        var url = "http://xnapi.lw.fminutes.com/api.php?inuId=" + savefarmer.inuId + "&method=Produce.produceFood";
        var postData = 'data={"produce_id":' + foodPlan + ',"id":' + id + '}&keyName=' + savefarmer.keyName + '&requestSig=' + savefarmer.requestSig;
        var result = this.request.doPostRequest(url, postData, fct, param);
        return this.tools.getJSON(result);
    }

    //打怪物
    Job_prototype.attackBeast = function(pointId, fId) {
        //累计api调用次数
        savefarmer.apiCount++;
        var url = "http://xnapi.lw.fminutes.com/api.php?inuId=" + savefarmer.inuId + "&method=Pve.attackBeast";
        var postData = 'data={"userName":"' + this.tools.changeName(savefarmer.initInfo.info.player_info.userName) + '","headPic":"' + savefarmer.initInfo.info.player_info.headPic + '","pointId":' + pointId + ',"fId":"' + fId + '"}&keyName=' + savefarmer.keyName + '&requestSig=' + savefarmer.requestSig;
        var result = this.request.doPostRequest(url, postData, null);
        return this.tools.getJSON(result);
    }

    //趁火打劫
    Job_prototype.loot = function(descId) {
        //累计api调用次数
        savefarmer.apiCount++;
        var url = "http://xnapi.lw.fminutes.com/api.php?inuId=" + savefarmer.inuId + "&method=Defence.loot";
        var postData = 'data={"desc_id":"' + descId + '"}&keyName=' + savefarmer.keyName + '&requestSig=' + savefarmer.requestSig;
        var result = this.request.doPostRequest(url, postData, null);
        return this.tools.getJSON(result);
    }

    //占领领地
    Job_prototype.defenceFight = function(desc_id, person, ids) {
        //累计api调用次数
        savefarmer.apiCount++;
        var url = "http://xnapi.lw.fminutes.com/api.php?inuId=" + savefarmer.inuId + "&method=Defence.fight";
        var postData = 'data={"desc_id":"' + desc_id + '","troops":{"90001":' + person + '},"ids":' + ids + '}&keyName=' + savefarmer.keyName + '&requestSig=' + savefarmer.requestSig;
        var result = this.request.doPostRequest(url, postData, null);
        return this.tools.getJSON(result);
    }

    //调整驻兵
    Job_prototype.adjustTroops = function(desc_id, person, ids) {
        //累计api调用次数
        savefarmer.apiCount++;
        var url = "http://xnapi.lw.fminutes.com/api.php?inuId=" + savefarmer.inuId + "&method=Defence.adjustTroops";
        var postData = 'data={"desc_id":"' + desc_id + '","troops":{"90001":' + person + '},"ids":' + ids + '}&keyName=' + savefarmer.keyName + '&requestSig=' + savefarmer.requestSig;
        var result = this.request.doPostRequest(url, postData, null);
        return this.tools.getJSON(result);
    }

    //解救好友
    /*
	Job_prototype.adjustTroops = function(desc_id, person, ids) {
        //累计api调用次数
        savefarmer.apiCount++;
        var url = "http://xnapi.lw.fminutes.com/api.php?inuId=" + savefarmer.inuId + "&method=Defence.adjustTroops";
        var postData = 'data={"desc_id":"' + desc_id + '","troops":{"90001":' + person + '},"ids":' + ids + '}&keyName=' + savefarmer.keyName + '&requestSig=' + savefarmer.requestSig;
        var result = this.request.doPostRequest(url, postData, null);
        return this.tools.getJSON(result);
    }
	*/

    //反抗
    Job_prototype.defenceRiot = function(heroId, troops, ids) {
        //累计api调用次数
        savefarmer.apiCount++;
        var url = "http://xnapi.lw.fminutes.com/api.php?inuId=" + savefarmer.inuId + "&method=Defence.riot";
        var postData = 'data={';
        if (heroId) {
            postData += '"heroId":' + heroId + ',"ids":' + ids + ',"troops":' + troops + ',"round":1}&keyName=' + savefarmer.keyName + '&requestSig=' + savefarmer.requestSig;
        } else {
            postData += '"ids":' + ids + ',"troops":' + troops + ',"round":1}&keyName=' + savefarmer.keyName + '&requestSig=' + savefarmer.requestSig;
        }
        var result = this.request.doPostRequest(url, postData, null);
        return this.tools.getJSON(result);
    }

    //生产原石
    Job_prototype.produceHero = function(produceId, id, fct, param) {
        //累计api调用次数
        savefarmer.apiCount++;
        //使用第一个英雄生产 
        var url = "http://xnapi.lw.fminutes.com/api.php?inuId=" + savefarmer.inuId + "&method=Produce.heroProduce";
        var postData = 'data={"heroId":' + 1 + ',"id":' + id + '}&keyName=' + savefarmer.keyName + '&requestSig=' + savefarmer.requestSig;
        var result = this.request.doPostRequest(url, postData, fct, param);
        return this.tools.getJSON(result);
    }

    //收获原石
    Job_prototype.gainHero = function(id, fct, param) {
        //累计api调用次数
        savefarmer.apiCount++;
        var url = "http://xnapi.lw.fminutes.com/api.php?inuId=" + savefarmer.inuId + "&method=Produce.gainHero";
        var postData = 'data={"id":' + id + '}&keyName=' + savefarmer.keyName + '&requestSig=' + savefarmer.requestSig;
        var result = this.request.doPostRequest(url, postData, fct, param);
        return this.tools.getJSON(result);
    }

    //拿取原石
    Job_prototype.stealingHero = function(id, descId, fct, param) {
        //累计api调用次数
        savefarmer.apiCount++;
        var url = "http://xnapi.lw.fminutes.com/api.php?inuId=" + savefarmer.inuId + "&method=Social.stealingHero";
        var postData = 'data={"id":' + id + ',"desc_id":"' + descId + '"}&keyName=' + savefarmer.keyName + '&requestSig=' + savefarmer.requestSig;
        var result = this.request.doPostRequest(url, postData, fct, param);
        return this.tools.getJSON(result);
    }
});